package com.it2297.controller;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import com.it2297.main.UserApp;
import com.it2297.model.Job;
import com.it2297.model.Needy;
import com.it2297.model.User;
import com.it2297.view.NeedyProfileView;
import com.it2297.view.NeedyView;
import com.it2297.view.UpdateNeedyView;
import com.it2297.view.NeedyDeleteView;
import com.it2297.view.NeedySearchView;

public class NeedyController {
	private NeedyView view;
	private UpdateNeedyView updatenv;
	private NeedyDeleteView deletenv;
	private NeedySearchView searchnv;
	private NeedyProfileView profilenv;
	private ArrayList<Needy> needyList = new ArrayList<Needy>();
	

	public NeedyController(NeedyView nv) {
		this.view = nv;
	}

	public NeedyController(UpdateNeedyView nv) {
		this.updatenv = nv;
	}

	public NeedyController(NeedyDeleteView nv) {
		this.deletenv = nv;
	}

	public NeedyController(NeedySearchView nv) {
		this.searchnv = nv;
	}
	
	public NeedyController(NeedyProfileView nv) {
		this.profilenv = nv;
	}
	
	
	public void CreateNeedy() {
		Needy needy = new Needy();
		User user = new User();
		user.setNric(view.getNeedyNric());
		needy.setNric(view.getNeedyNric());
		user.setName(view.getNeedyName());
		user.setAddress(view.getNeedyAddress());
		user.setPhone(view.getNeedyPhone());
		user.setPassword(view.getNeedyPassword());
		user.setGender(view.getNeedyGender());

		user.setEmail(view.getNeedyEmail());
		user.setEthnic(view.getNeedyEthnic());
	    user.setDob(view.getNeedyDob());
	    
		user.setStatus("ava");
		user.setCountryOfBirth(view.getNeedyCountryOfBirth());
		needy.setHouseHoldIncome(view.getNeedyHouseHoldIncome());
		needy.setFamilySize(view.getNeedyFamilySize());
		needy.setNumOfDependent(view.getNeedyNumOfDependent());

		
		
		if(!Needy.IsNricExist(view.getNeedyNric())){
			Needy.createUser(user);
			Needy.createNeedy(needy);
				JOptionPane.showMessageDialog(null, "Needy Resident personal records created successfully!");
				}
		else{
			JOptionPane.showMessageDialog(null, "Duplicate Nric, Unable to create.");
		}
		
	}

	
	public void UpdateNeedy() {

		Needy needy = new Needy();
		User user = new User();

		user.setNric(updatenv.getNeedyNric());
		user.setName(updatenv.getNeedyName());
		user.setAddress(updatenv.getNeedyAddress());
		user.setPhone(updatenv.getNeedyPhone());
		user.setPassword(updatenv.getNeedyPassword());
		user.setGender(updatenv.getNeedyGender());

		user.setEmail(updatenv.getNeedyEmail());
		user.setEthnic(updatenv.getNeedyEthnic());
		user.setDob(updatenv.getNeedyDob());
		user.setStatus(updatenv.getNeedyStatus());
		user.setCountryOfBirth(updatenv.getNeedyCountryOfBirth());

		needy.setHouseHoldIncome(updatenv.getNeedyHouseHoldIncome());
		needy.setFamilySize(updatenv.getNeedyFamilySize());
		needy.setNumOfDependent(updatenv.getNeedyNumOfDependent());

		boolean isSuccessful = Needy.IsNricExist2(updatenv.getNeedyNric());
		
		if(isSuccessful){
			Needy.updateUser(user);
			Needy.updateNeedy(needy);
			User user2 = User.retrieveDetails(updatenv.getNeedyNric());
			UserApp.setCurrentUser(user2);
			JOptionPane.showMessageDialog(null, "Record updated successfully!");
		}
		else{
			JOptionPane.showMessageDialog(null, "User does not exist!");
			updatenv.setNeedyPassword("");
		}
		
		
		
		
		
		
		

	}

	public void getNeedy(String nric) {
		Needy needy = new Needy();
		needy.getSingle(nric);

		updatenv.setNeedyName(needy.getName());
		updatenv.setNeedyAddress(needy.getAddress());
		updatenv.setNeedyPhone(needy.getPhone());
		updatenv.setNeedyPassword(needy.getPassword());
		updatenv.setNeedyGender(needy.getGender());
		updatenv.setNeedyEmail(needy.getEmail());
		updatenv.setNeedyEthnic(needy.getEthnic());
		updatenv.setNeedyDob(needy.getDob());
		updatenv.setNeedyStatus(needy.getStatus());
		updatenv.setNeedyCountryOfBirth(needy.getCountryOfBirth());
		updatenv.setNeedyHouseHoldIncome(needy.getHouseHoldIncome());
		updatenv.setNeedyFamilySize(needy.getFamilySize());
		updatenv.setNeedyNumOfIndependent(needy.getNumOfDependent());

	}

	public void getNeedyDetail(String nric) {
		Needy needy = new Needy();
		needy.getSingle(nric);

		deletenv.setNeedyName(needy.getName());
		deletenv.setNeedyAddress(needy.getAddress());
		deletenv.setNeedyPhone(needy.getPhone());
		deletenv.setNeedyPassword(needy.getPassword());
		deletenv.setNeedyGender(needy.getGender());
		deletenv.setNeedyEmail(needy.getEmail());
		deletenv.setNeedyEthnic(needy.getEthnic());
		deletenv.setNeedyDob(needy.getDob());
		deletenv.setNeedyStatus(needy.getStatus());
		deletenv.setNeedyCountryOfBirth(needy.getCountryOfBirth());
		deletenv.setNeedyHouseHoldIncome(needy.getHouseHoldIncome());
		deletenv.setNeedyFamilySize(needy.getFamilySize());
		deletenv.setNeedyNumOfIndependent(needy.getNumOfDependent());
	}

	public void getProfileDetail(String nric) {
		Needy needy = new Needy();
		needy.getSingle(nric);

		profilenv.setNeedyName(needy.getName());
		profilenv.setNeedyAddress(needy.getAddress());
		profilenv.setNeedyPhone(needy.getPhone());
		profilenv.setNeedyPassword(needy.getPassword());
		profilenv.setNeedyGender(needy.getGender());
		profilenv.setNeedyEmail(needy.getEmail());
		profilenv.setNeedyEthnic(needy.getEthnic());
		profilenv.setNeedyDob(needy.getDob());
		profilenv.setNeedyStatus(needy.getStatus());
		profilenv.setNeedyCountryOfBirth(needy.getCountryOfBirth());
		profilenv.setNeedyHouseHoldIncome(needy.getHouseHoldIncome());
		profilenv.setNeedyFamilySize(needy.getFamilySize());
		profilenv.setNeedyNumOfIndependent(needy.getNumOfDependent());
	}
	
	
	public void updateStatusToDel() {
		User user = new User();
		user.setNric(deletenv.getNeedyNric());
		Needy.updateStatusToDel(user);
	}

	
	
	
	public void generateList(){
		searchnv.getPanel().removeAll();
		
		Needy needy = new Needy();
		needy.setName(searchnv.getSearchTxt().getText());
		needyList = Needy.retrieveSearch(needy);
		
		searchnv.getPanel().setPreferredSize(new Dimension(580, needyList.size() * 30));
		
		for(int i = 0; i < needyList.size(); i++){
			final JPanel needyPanel = new JPanel();
			
			needyPanel.addMouseListener(new MouseListener(){

				@Override
				public void mouseClicked(MouseEvent e) {
					//JDialog
				}

				@Override
				public void mouseEntered(MouseEvent e) {
				}

				@Override
				public void mouseExited(MouseEvent e) {
				}

				@Override
				public void mousePressed(MouseEvent e) {
				}

				@Override
				public void mouseReleased(MouseEvent e) {
				}
				
			});

			JLabel Name = new JLabel(needyList.get(i).getName());
			JLabel Nric = new JLabel(needyList.get(i).getNric());
			//JSeparator separator = new JSeparator();
			
			needyPanel.setPreferredSize(new Dimension(580,30));

		//	Name.setPreferredSize(new Dimension(100,30));
			Name.setBounds(0, 5, 100, 30);
			needyPanel.add(Name);
		//	Nric.setPreferredSize(new Dimension(100,30));
			Nric.setBounds(300, 5, 100, 30);
			needyPanel.add(Nric);
			//separator.setPreferredSize(new Dimension(300,5));
			//needyPanel.add(separator);
			
			searchnv.getPanel().add(needyPanel);
		}
		
		
		
		
		
	}
	
/*
	public ArrayList<Needy> searchForMatch(){
		Needy needy = new Needy();
		needy.setName(searchnv.getName());
		ArrayList<Needy> match= Needy.retrieveSearch(needy);
		searchnv.setName(needy.getName());
		return match;
		
	}
	*/
	
	
}

